package com.ruicar.afs.cloud.basic.common.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.basic.common.condition.BasicReceiptRecordCondition;
import com.ruicar.afs.cloud.basic.common.condition.ContractStatusCondition;
import com.ruicar.afs.cloud.basic.common.entity.BasicReceiptRecord;
import com.ruicar.afs.cloud.basic.proceeds.debit.entity.BasicDebitBatchDetails;
import com.ruicar.afs.cloud.basic.proceeds.debit.service.BasicDebitBatchDetailsService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: xudongdong
 * @date: 2020/7/15
 * @description: 批次明细controller
 */
@Slf4j
@RestController
@RequestMapping("/debitBatchDetails")
@AllArgsConstructor
public class BasicDebitBatchDetailsController {

    private final BasicDebitBatchDetailsService basicDebitBatchDetailsService;

    @ApiOperation(value = "更新状态")
    @PostMapping("/updateContractStatus")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<Boolean> updateContractStatus(@RequestBody ContractStatusCondition contractStatus) {
        basicDebitBatchDetailsService.update(Wrappers.<BasicDebitBatchDetails>lambdaUpdate().eq(BasicDebitBatchDetails::getContractNo, contractStatus.getContractNo())
                .set(BasicDebitBatchDetails::getContractStatus, contractStatus.getContractStatus()));
        return new IResponse().setData(true);
    }

    @ApiOperation(value = "根据收款来源id查询来源表数据")
    @PostMapping("/getDebitBatchDetails")
    public IResponse getDebitBatchDetails(@RequestBody List<BasicReceiptRecordCondition> list) {
        for (BasicReceiptRecordCondition condition : list) {
            BasicDebitBatchDetails debitBatchDetails = basicDebitBatchDetailsService.getById(condition.getSourceId());
            if (EmptyUtils.isNotEmpty(debitBatchDetails)) {
                condition.setUserName(debitBatchDetails.getDebitAccountName());
                condition.setSummary(debitBatchDetails.getRemarks());
            }
        }
        return new IResponse().setData(list);
    }
}
